package com.mogujie.tt.ui.activity;


import com.mogujie.tt.DB.DBInterface;
import com.mogujie.tt.DB.entity.UserEntity;
import com.mogujie.tt.R;
import com.mogujie.tt.config.NewFriendsConstant;
import com.mogujie.tt.imservice.event.NewFriendsEvent;
import com.mogujie.tt.imservice.manager.IMNewFriendsManager;
import com.mogujie.tt.imservice.support.IMServiceConnector;
import com.mogujie.tt.ui.adapter.NewFriendsAdapter;
import com.mogujie.tt.ui.base.QZBaseActivity;
import com.mogujie.tt.ui.base.TTBaseActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * 申请与通知
 * 
 */
public class NewFriendsActivity extends QZBaseActivity {
    private ListView listView;
    NewFriendsAdapter adapter;
    private IMServiceConnector imServiceConnector = new IMServiceConnector() {
        @Override
        public void onIMServiceConnected() {

            EventBus.getDefault().registerSticky(NewFriendsActivity.this);//注册事件
        }

        @Override
        public void onServiceDisconnected() {
            if (EventBus.getDefault().isRegistered(NewFriendsActivity.this)) {
                EventBus.getDefault().unregister(NewFriendsActivity.this);
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        imServiceConnector.connect(this);
        LayoutInflater.from(this).inflate(R.layout.tt_activity_newfriendsmsg, topContentView);//加载布局
        showTopInformation("新的朋友");
        listView = (ListView) findViewById(R.id.listview);//请求添加你的列表
        TextView et_search = (TextView) findViewById(R.id.et_search);//查找人

        et_search.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
             startActivity(new Intent(NewFriendsActivity.this, AddFriendsTwoActivity.class));
                Log.v("NewFriendsActivity","et_search.setOnClickListener ------------");
            }

        });

        // 设置adapter
        adapter = new NewFriendsAdapter(this,IMNewFriendsManager.instance().loadRequestAddFriendDatabase());
        listView.setOnItemClickListener(adapter);
        listView.setAdapter(adapter);


    }

    /**
     * 退出添加好友
     * @param v
     */
    public void onClickExit(View v) {
        finish();
    }

    /**
     * 显示顶部信息
     */
    private void showTopInformation(String test){
        if(test == null){
            return;
        }
        setTopBarDisplay();
        setCenterText(test);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        onDest();
        imServiceConnector.disconnect(this);
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    /**-----------事件--------------------------**/
    public void onEventMainThread(NewFriendsEvent event) {
        switch (event.getEvent()) {
            case REFRESH_REQUEST_FRIENDS:
                addFriendAfterLocalUpdates();
                break;

        }
    }

    /**
     * 添加好友后的本地更新
     */
    private void addFriendAfterLocalUpdates(){

        adapter.putUserList(IMNewFriendsManager.instance().loadRequestAddFriendDatabase());
    }

}
